package ru.ifmo.ipimenov.ewf_svn.standalone.controller;

import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNUpdateClient;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNCommitClient;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.auth.BasicAuthenticationManager;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;

import ru.ifmo.ipimenov.ewf_svn.standalone.model.list.Model;
import ru.ifmo.ipimenov.ewf_svn.standalone.model.list.Item;

/**
 * @author: Ilya Pimenov
 * Scartel Starlab company
 */
public class ProcessAction implements ActionListener {
    /**
     * Logger for all purposes
     */
    private static Log log = LogFactory.getLog(ProcessAction.class);

    private JList list;

    public ProcessAction(JList list) {
        this.list = list;
    }

    public void actionPerformed(ActionEvent e) {
        JFileChooser c = new JFileChooser();
        c.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        // Demonstrate "Save" dialog
        int rVal = c.showSaveDialog(list);
        if (rVal == JFileChooser.APPROVE_OPTION) {
            log.debug(c.getSelectedFile().getName());
            File f = c.getSelectedFile();
            exportSelected(f);
            log.info("Checked out to " + f.getAbsolutePath());
        }
        if (rVal == JFileChooser.CANCEL_OPTION) {
            log.debug("You pressed cancel");
        }
    }

    private void exportSelected(File dest) {
        SVNClientManager clientManager = SVNClientManager.newInstance();

        BasicAuthenticationManager iam = new BasicAuthenticationManager("ilya.pimenov", "n7s9m9d3");
        iam.setAuthenticationForced(true);

        SVNUpdateClient client = new SVNUpdateClient(iam, null);
        DAVRepositoryFactory.setup();
        SVNRepositoryFactoryImpl.setup();
        FSRepositoryFactory.setup();
        if (!(list.getModel() instanceof Model)) {
            log.error("Please acquire information first");
            return;
        }
        Model m = (Model) list.getModel();
        Item i = m.getItem(list.getSelectedIndex());
        i.setStatus(Item.PROCESSING);
        SVNURL url = i.getUrl();
        i.setFile(new File(dest, i.getName()));
        client.setIgnoreExternals(true);
        try {
            client.doExport(url, dest, SVNRevision.HEAD, SVNRevision.HEAD, "native", false, false);
        } catch (SVNException e) {
            log.error("Failed to checkout from svn server. " + e);
        }
    }
}
